function changeTime(iNum) {
    iNum = parseInt(iNum)
    var iH = parseInt(iNum / 3600).toString().padStart(2, '0');
    var iM = parseInt(iNum % 3600 / 60).toString().padStart(2, '0');
    var iS = parseInt(iNum % 60).toString().padStart(2, '0');
    return iH + ":" + iM + ":" + iS
}

var oA = document.querySelector('#oA');
var allTime = document.querySelector('.allTime')
var onoFF = document.querySelector('.onoff')
var current = document.querySelector('.current');
var bar = document.querySelector('.bar');
var line = document.querySelector('.line');
var topp = document.querySelector('.top');
var timer;

line.onclick = function(ev) {
    var l = ev.clientX;
    var end = this.offsetWidth - 5
    if (l < end) {
        bar.style.left = l + 'px';
        oA.currentTime = (l / end) * oA.duration;
    }
    if (l > end) {
        bar.style.left = end + 'px';
        oA.currentTime = oA.duration;
    }
    current.innerHTML = changeTime(oA.currentTime);
}

oA.oncanplay = function() {
    allTime.innerHTML = changeTime(oA.duration)
}

onoFF.addEventListener('click', function() {
    oA.play();
    this.classList.toggle('glyphicon-pause')
    topp.style.animationPlayState = 'running';
    if (timer) {
        oA.pause();
        audioPause();
    } else {
        timer = setInterval(function() {
            current.innerHTML = changeTime(oA.currentTime);
            bar.style.left = parseInt(oA.currentTime / oA.duration * line.offsetWidth) + 'px';
            if (oA.ended) {
                audioPause();
            }
        }, 1000)
    }
}, false);
// 播放完成执行
function audioPause() {
    timer = clearInterval(timer);
    topp.style.animationPlayState = 'paused';
    onoFF.classList.remove('glyphicon-pause');
}

document.querySelector('.back').onclick = function() {
    history.go(-1);
}

// 获取歌曲id
var musicId = location.search.replace(/\D/g, '');
console.log(musicId);

ajax({
    url: '/song/url',
    method: 'GET',
    data: {
        id: musicId,
    },
    success: function(resp) {
        const data = resp.data[0];
        document.querySelector('audio').src = data.url;
    }
});

ajax({
    url: '/song/detail',
    method: 'GET',
    data: {
        ids: musicId,
    },
    success: function(resp) {
        const data = resp.songs[0];
        topp.style.backgroundImage = `url(${data.al.picUrl})`;
        document.querySelector('.title').innerHTML = data.name + (data.alia[0] ? `(${data.alia[0]})` : '')
    }
});

document.querySelector('#back').onclick = function() {
    var m = Number(localStorage.getItem("n")) - 1;
    if (m < 0) {
        m = 0;
        alert("已经是第一首了")
    } else {
        localStorage.setItem('n', m);
        location.search = localStorage.getItem(`musicId${m}`);
    }
}

document.querySelector('#next').onclick = function() {
    var m = Number(localStorage.getItem("n")) + 1;
    if (m > 29) {
        m = 29;
        alert("已经是最后一首了")
    } else {
        localStorage.setItem('n', m);
        location.search = localStorage.getItem(`musicId${m}`);
    }
}